package cn.cool.domain.activity.service;

import cn.cool.domain.activity.adapter.repository.IActivityRepository;
import cn.cool.domain.activity.model.entity.MarketProductEntity;
import cn.cool.domain.activity.model.entity.TrialBalanceEntity;
import cn.cool.domain.activity.model.entity.UserGroupBuyOrderDetailEntity;
import cn.cool.domain.activity.model.valobj.TeamStatisticVO;
import cn.cool.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import cn.cool.domain.trade.adapter.repository.ITradeRepository;
import cn.cool.types.design.framework.tree.StrategyHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class IIndexGroupBuyMarketServiceImpl implements IIndexGroupBuyMarketService {
    @Resource
    private DefaultActivityStrategyFactory defaultActivityStrategyFactory;

    @Resource
    private IActivityRepository repository;

    @Override
    public TrialBalanceEntity indexMarketTrial(MarketProductEntity marketProductEntity) throws Exception {
        StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> strategyHandler =
                defaultActivityStrategyFactory.strategyHandler();
        return strategyHandler.apply(marketProductEntity, new DefaultActivityStrategyFactory.DynamicContext());
    }

    @Override
    public List<UserGroupBuyOrderDetailEntity> queryInProgressUserGroupBuyOrderDetailList(Long activityId, String userId, Integer ownerCount, Integer randomCount) {
        List<UserGroupBuyOrderDetailEntity> unionAll = new ArrayList<>();

        if (0 != ownerCount) {
            List<UserGroupBuyOrderDetailEntity> ownerList = repository.queryInProgressUserGroupBuyOrderDetailListByOwner(activityId, userId, ownerCount);
            if (ownerList != null && !ownerList.isEmpty()) {
                unionAll.addAll(ownerList);
            }
        }

        if (randomCount != 0) {
            List<UserGroupBuyOrderDetailEntity> randomList = repository.queryInProgressUserGroupBuyOrderDetailListByRandom(activityId, userId, ownerCount);
            if (randomList != null && !randomList.isEmpty()) {
                unionAll.addAll(randomList);
            }
        }

        return unionAll;
    }

    @Override
    public TeamStatisticVO queryTeamStatisticByActivityId(Long activityId) {
        return repository.queryTeamStatisticByActivityId(activityId);
    }

}
